const CONFIG = { //缓存配置
    config: "config", //站点配置
    token: "token", //有效期
    userInfo: "userInfo", //用户信息
    regions: "regions",  //区域信息
    userType: "userType" //用户类型
}
/*
* 缓存处理方法
* */

//异步存储
function set(name, data) {
    return _setStorage(name, data);
}

//同步存缓存
function setSync(name, data) {
    return _setStorage(name, data, "sync");
}

//同步存缓存 -- 获取后即销毁
function getSyncOnce(name) {
    return _getStorage(name, "once")
}

//同步取缓存
function getSync(name) {  //同步存
    return _getStorage(name)
}

//异步清楚
function remove(name) {
    _removeStorage(name)
}

//同步清除
function removeSync(name) {
    _removeStorage(name, "sync")
}

/*
* 解析参数
* 设置缓存信息
* */
function _setStorage(name, data, type) {
    try {
        let keys = name.split(".") || [];
        let storageKeyName = (keys[0] && CONFIG[keys[0]]) ? CONFIG[keys[0]] : "";
        let childKeys = null;
        let currentValue = null;
        let storage = null;
        if (!storageKeyName) {throw new Error("缓存存储失败，未配置缓存名" + name)}
        if (keys.length === 1) {
            currentValue = data;
        } else {
            storage = wx.getStorageSync(storageKeyName) || {};
            let valueArr = [];
            for (let i = 0; i < keys.length; i ++) {
                if (i === 0) {
                    valueArr.push(storage);
                } else if (i !== keys.length - 1) { //不是最后一个
                    valueArr.push(storage[keys[i]] || {});
                    storage = storage[keys[i]] || {};
                } else {
                    valueArr.push(data || "");
                }
            }
            childKeys = keys.splice(1, keys.length - 1);
            childKeys.reverse(); //倒序
            valueArr.reverse(); //倒序
            for (let p = 0; p < childKeys.length; p ++) {
                valueArr[p + 1][childKeys[p]] = valueArr[p]
            }
            currentValue = valueArr[valueArr.length - 1]
        }
        if (type === "sync") {
            wx.setStorageSync(storageKeyName, currentValue);
        } else {
            wx.setStorage({
                key: storageKeyName,
                data: currentValue
            });
        }
        return currentValue;
    } catch (e) {
        console.error(e)
    }
}

/*
* 解析参数
* 获取缓存信息
* */
function _getStorage(name, type) {
    try {
        let keys = name.split(".") || [];
        let storageKeyName = (keys[0] && CONFIG[keys[0]]) ? CONFIG[keys[0]] : "";
        let childKeys = null;
        let currentValue = null;
        let valueArr = [];
        if (!storageKeyName) {throw new Error("缓存获取失败，未配置缓存名" + name)}
        let storage = wx.getStorageSync(storageKeyName) || "";
        if (keys.length === 1) {
            currentValue = storage
        } else {
            for (let i = 0; i < keys.length; i ++) {
                if (i === 0) {
                    valueArr.push(storage);
                } else {
                    if (!storage) {
                        currentValue = storage;
                        break
                    } else {
                        valueArr.push(storage[keys[i]]);
                        storage = storage[keys[i]];
                        currentValue = storage;
                    }
                }
            }
        }
        if (type === "once") { //读取完缓存即清空
            if (keys.length === 1) {
                wx.removeStorage({key: storageKeyName})
            } else if (valueArr.length === keys.length) { //最后字段值存在
                childKeys = keys.splice(1, keys.length - 1);
                childKeys.reverse(); //倒序
                valueArr.reverse(); //倒序
                for (let p = 0; p < childKeys.length; p ++) {
                    if (p === 0) {
                        delete valueArr[p + 1][childKeys[p]]
                    } else {
                        valueArr[p + 1][childKeys[p]] = valueArr[p]
                    }
                }
                wx.setStorage({
                    key: storageKeyName,
                    data: valueArr[valueArr.length - 1]
                });
            }
        }
        return currentValue || ""
    } catch (e) {
        console.error(e)
    }
}


/*
* 解析参数
* 删除缓存信息
* */

function _removeStorage(name, type) {
    try {
        let keys = name.split(".") || [];
        let storageKeyName = (keys[0] && CONFIG[keys[0]]) ? CONFIG[keys[0]] : "";
        let storage = null;
        let childKeys = null;
        let valueArr = [];
        if (!storageKeyName) {throw new Error("缓存删除失败，未配置缓存名" + name)}
        if (keys.length === 1) {
            if (type === "sync") {
                wx.removeStorageSync(storageKeyName)
            } else {
                wx.removeStorage({key: storageKeyName})
            }
        } else {
            storage = wx.getStorageSync(storageKeyName) || {};
            for (let i = 0; i < keys.length; i ++) {
                if (i === 0) {
                    valueArr.push(storage);
                } else {
                    if (!storage) {
                        break
                    } else {
                        valueArr.push(storage[keys[i]]);
                        storage = storage[keys[i]];
                    }
                }
            }
            if (valueArr.length === keys.length) { //最后字段值存在
                childKeys = keys.splice(1, keys.length - 1);
                childKeys.reverse(); //倒序
                valueArr.reverse(); //倒序
                for (let p = 0; p < childKeys.length; p ++) {
                    if (p === 0) {
                        delete valueArr[p + 1][childKeys[p]]
                    } else {
                        valueArr[p + 1][childKeys[p]] = valueArr[p]
                    }
                }
                if (type === "sync") {
                    wx.setStorageSync(storageKeyName, valueArr[valueArr.length - 1]);
                } else {
                    wx.setStorage({
                        key: storageKeyName,
                        data: valueArr[valueArr.length - 1]
                    });
                }
            }
        }
    } catch (e) {
        console.error(e)
    }
}

module.exports = {
    setStorage: set,
    setStorageSync: setSync,
    getStorageSyncOnce: getSyncOnce,
    getStorageSync:getSync,
    removeStorage: remove,
    removeStorageSync: removeSync
}